package org.jeecg.modules.workDaily.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.workDaily.entity.WorkDaily;
import org.jeecg.modules.workDaily.mapper.WorkDailyMapper;
import org.jeecg.modules.workDaily.service.IWorkDailyService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 工作日报
 * @Author: jeecg-boot
 * @Date:   2023-09-06
 * @Version: V1.0
 */
@Service
public class WorkDailyServiceImpl extends ServiceImpl<WorkDailyMapper, WorkDaily> implements IWorkDailyService {
    /**
     * 工作日报过期更新
     */
    public void task(){
        String startTimeStr = getToDayStartTimeStr();//今天开始时间
        /*List<WorkDaily> yesterday = getWorkDailies(0, startTimeStr);
        yesterday.stream().map(t->{
            t.setWorkDay(-1);
            this.updateById(t);
            return t;
        }).collect(Collectors.toList());*/
        List<WorkDaily> today = getWorkDailies(1, startTimeStr);
        today.stream().map(t->{
            t.setWorkDay(0);
            this.updateById(t);
            return t;
        }).collect(Collectors.toList());
        List<WorkDaily> tomorrow = getWorkDailies(2, startTimeStr);
        tomorrow.stream().map(t->{
            t.setWorkDay(1);
            this.updateById(t);
            return t;
        }).collect(Collectors.toList());
    }

    /**
     * 查询指定工作时间的工作日报
     * @param workDay
     * @return
     */
    private List<WorkDaily> getWorkDailies(int workDay, String todayTime) {
        LambdaQueryWrapper<WorkDaily> qwr = Wrappers.lambdaQuery();
        qwr.eq(WorkDaily::getWorkDay,workDay).lt(WorkDaily::getCreateTime,todayTime);
        return this.list(qwr);
    }

    /**
     * 获取当天最小时间  String类型(yyyy-MM-dd HH:mm:ss)
     *
     * @return String
     */
    public static String getToDayStartTimeStr() {
        //获取当前时间-在这里可以增加或者减少时间求出非当前天的最小时间
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime toDay = localDateTime.with(LocalTime.MIN);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return toDay.format(dateTimeFormatter);
    }
}
